﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class Player: Character
    {
        private int driveOffSetX = 10, driveOffSetY = 40, power = 0, maxPower, particleLevel = 1;
        private List<AbilitySet> setofAbiities = new List<AbilitySet>();
        private List<GNParticles> gnDriveParticles;
        private float heat;
        private Bitmap HUD = Resources.LoadBitmap(Resources.PathToResource("CharSprites\\HUD.png"));
        
        public int ParticleLevel
        {
            get { return particleLevel; }
            set { particleLevel = value; }
        }

        public float Heat
        {
            get { return heat; }
            set { heat = value; }
        }

        public int MaxPower
        {
            get { return maxPower; }
            set { maxPower = value; }
        }

        public int Power
        {
            get { return power; }
            set { power = value; }
        }

        public int DriveOffSetY
        {
            get {return driveOffSetY;}
        }

        public int DriveOffSetX()
        {
            if (State == GlobalConst.SpriteState.normalWest || State == GlobalConst.SpriteState.transAmWest)
                return 10;
            else
                return -30;
        }

        public List<GNParticles> GnDriveParticles
        {
            get { return gnDriveParticles; }
            set { gnDriveParticles = value; }
        }

        public List<AbilitySet> SetofAbiities
        {
          get { return setofAbiities; }
          set { setofAbiities = value; }
        }

        public Player(string name, Sprite gundamSprite,int shootOffSetX,int shootOffsetY, int health, AI ai)
            :base(name,gundamSprite,shootOffSetX, shootOffsetY, health, ai)
        {  }

        public override void Update(GameController gc)
        {
            base.Update(gc);

            DoAbility(gc);
            if (gnDriveParticles != null)
                for (int i = 0; i < gnDriveParticles.Count; i++)
                    gnDriveParticles[i].Update((int)(objectSprite.X + objectSprite.Width / 2 + DriveOffSetX()),
                        (int) (objectSprite.Y + objectSprite.Height / 2 - driveOffSetY));
        }

        public override void Draw()
        {
            base.Draw();
            if (gnDriveParticles != null)
                for (int i = 0; i < 100*particleLevel-1; i++)
                    gnDriveParticles[i].Draw();
            DrawUI();
        }

        public void DoAbility(GameController gc)
        {
            ManageHeat(gc);
            foreach (Ability a in CurrentSet.SetOfAbilities)
            {
                if (a.Name.Contains("Move"))
                    Movement(a, gc);
                else if (Input.KeyTyped(a.K) && !a.IsActive || a.IsActive)
                    a.Update(gc);
            } 
        }

        public void Movement(Ability a, GameController gc)
        {
            if ((GlobalLockout || !Input.KeyDown(a.K)) && a.IsActive)
            {
                LockoutTiers[0] = false;
                a.IsActive = false;
                if (!GlobalLockout && !Input.KeyDown(a.K))
                    GlobalConst.Utils.ActivateAnimationRow(ObjectSprite, offSetSpriteRow(), GlobalConst.DefaultColEnd, GlobalConst.DefaultFPC);
                return;
            }
            if (GlobalLockout || !Input.KeyDown(a.K)) return;
            if (Input.KeyDown(a.K) && !LockoutTiers[0])
            {
                a.Update(gc);
                LockoutTiers[0] = true;
            }
            else if (Input.KeyDown(a.K) && a.IsActive)
                a.Update(gc);
        }

        public void DrawUI()
        {
            Graphics.FillRectangle(Color.Red, 110, 28, (int)((((Health - maxHealth) + maxHealth) * 200) / maxHealth), 20);
            Graphics.FillRectangle(Color.Lime, 110, 61, (int)((((power - maxPower) + maxPower) * 200) / maxPower), 20);

            if (heat < 800)
                Graphics.FillRectangle(Color.Yellow, 50, 90,(int)((((heat - 1000) + 1000) * 268) / 1000),10);
            else
                Graphics.FillRectangle(Color.Crimson, 50, 90,(int)((((heat - 1000) + 1000) * 268) / 1000),10);

            int posX = 1100, posY = 5;
            foreach (BuffAction b in Buffs)
            {
                b.DrawBuffDetails(posX, posY);
                posY += 30;
            }

            HUD.Draw(0, 0);
            posX = 360; posY = 5;
            foreach (Ability a in CurrentSet.SetOfAbilities)
            {
                if (!a.Name.Contains("Move"))
                {
                    a.Draw(posX, posY);
                    posX += 80;
                } 
            }
            Text.DrawText(Name, Color.White, 135, 5);
        }

        private void ManageHeat(GameController gc)
        {
            if (heat > 0) --heat;
            heat += (float)particleLevel / 4f;
            if (heat > 1000) TakeDamage(200, gc);
        }
    }
}
